/**
 * Author: grx
 * Description: 学习模块的子功能选择Fragment,包含"开始学习"和"学习统计"两个子功能
 */
package com.example.parallelandroid;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class StudyFragment extends Fragment {

    private Button btnStartStudy;
    private Button btnStudyStatistics;
    private AnimationDrawable adStudy,adSta;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 获取布局文件
        View view = inflater.inflate(R.layout.fragment_study, null);
        // 获取布局文件中的控件对象
        btnStartStudy = view.findViewById(R.id.btn_startStudy);
        btnStudyStatistics = view.findViewById(R.id.btn_studyStatistics);
        // 给btn设置字体
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(),"包图小白体.ttf");
        btnStartStudy.setTypeface(typeface);
        btnStudyStatistics.setTypeface(typeface);
        // 设置渐变色
        LinearGradient blue_linearGradient=new LinearGradient(0, 0, 0, btnStartStudy.getPaint().getTextSize(), Color.BLUE, Color.BLACK, Shader.TileMode.CLAMP);
        LinearGradient redLinearGradient = new LinearGradient(0, 0, 0, btnStartStudy.getPaint().getTextSize(), Color.BLACK, Color.RED, Shader.TileMode.CLAMP);
        btnStartStudy.getPaint().setShader(blue_linearGradient);
        btnStudyStatistics.getPaint().setShader(redLinearGradient);
        //为button添加渐变动画
        adStudy = (AnimationDrawable) btnStartStudy.getBackground();
        Handler handler1 = new Handler();
        handler1.postDelayed(new Runnable() {
            @Override
            public void run() {
                adStudy.start();
            }
        }, 300);
        adSta = (AnimationDrawable) btnStudyStatistics.getBackground();
        Handler handler2 = new Handler();
        handler2.postDelayed(new Runnable() {
            @Override
            public void run() {
                adSta.start();
            }
        }, 300);

        // 给控件对象绑定监听器
        setListeners();
        // 返回布局文件对象
        return view;
    }

    // 给控件对象绑定监听器
    private void setListeners() {
        MyListener myListener = new MyListener();
        btnStartStudy.setOnClickListener(myListener);
        btnStudyStatistics.setOnClickListener(myListener);
    }

    private class MyListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_startStudy:
                    // 跳转到"开始学习"页面
                    Intent intent1 = new Intent();
                    intent1.setClass(getContext(), StartStudyActivity.class);
                    startActivity(intent1);
                    Constant.logger.info("Jump from StudyFragment to StartStudyActivity");
                    break;
                case R.id.btn_studyStatistics:
                    // 跳转到"学习统计"页面
                    Intent intent2 = new Intent();
                    intent2.setClass(getContext(), StudyStatisticsActivity.class);
                    startActivity(intent2);
                    Constant.logger.info("Jump from StudyFragment to StartStudyActivity");
                    break;
            }
        }
    }
}
